package
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.display.MovieClip;
	import flash.utils.getTimer;
	import flash.ui.Keyboard;
	import flash.system.System;
	import flash.text.TextField;
	import flash.utils.getDefinitionByName;
	import flash.display.DisplayObject;

	public class Main extends Container
	{
		private static var instance:Main;
		public var console:ConsoleObject;
		public var gfx:NinjaGraphicsSystem;
		public var tiles:TileMap;
		public var objects:ObjectManager;
		public var userdata:NinjaUserData;
		public var game:NinjaGame;
		public var editor:NinjaEditor;
		private var gamedata:NinjaData;
		public var APP_GAMETIME_TICKLEN:Number;
		public var APP_TILE_SCALE:Number;
		public var APP_KEY_PRESSED:Boolean;
		public var APP_KEY_TRIG:Boolean;
		public var GRAV:Number;
		public var DRAG:Number;
		public var particles:ParticleManager;
		public var mcRend:VectorRenderer;
		public var input:InputManager;
		public var mcBuffer:Container;
		public var static_rend:VectorRenderer;
		public var debug_rend:VectorRenderer;
		public var gui:NinjaGUI;
		public var BOUNCE:Number;
		public var FRICTION_THRESHOLD:Number;
		public var FRICTION_STATIC:Number;
		public var FRICTION_DYNAMIC_RATIO:Number;
		public var APP_t0:Number;
		public var APP_FPSBOX:MovieClip;
		public var App_PlayerDeathEvent:Function;
		public var App_LevelPassedEvent:Function;
		public var APP_PRE_QUIT:Boolean;
		public var App_UpdateHelp:Function;
		public var APP_HELPLEVEL_LOADED:Boolean;

		public function Main()
		{
			if (Main.instance == null)
			{
				ProjAABB_Tile.initialize();
				ProjCircle_Tile.initialize();
				TestPoint_Tile.initialize();
				TestRay_Tile.initialize();

				this.stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyManager.onKeyDown);
				this.stage.addEventListener(KeyboardEvent.KEY_UP, KeyManager.onKeyUp);

				Main.instance = this;
				// fscommand("allowscale", "false");
				// fscommand("showmenu", "false");
				this.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown);
				this.stage.addEventListener(KeyboardEvent.KEY_UP, this.onKeyUp);
				//
				this.APP_GAMETIME_TICKLEN = 25;
				this.APP_TILE_SCALE = 12;
				InitApp();
			}
		}

		public static function getInstance():Main
		{
			return Main.instance;
		}

		public function InitApp():void
		{
			gfx = new NinjaGraphicsSystem();
			particles = new ParticleManager(this.gfx.bufferList[NinjaGraphicsSystem.LAYER_PARTICLES_FRONT], this.gfx.bufferList[NinjaGraphicsSystem.LAYER_PARTICLES_BACK]);
			mcRend = new VectorRenderer();
			mcBuffer = mcRend.buffer;
			// 我添加的
			this.static_rend = new VectorRenderer();
			this.debug_rend = new VectorRenderer();
			//
			input = new InputManager();
			GRAV = 0.15;
			// gravity(重力)
			DRAG = 0.999999;
			// drag(阻力)
			BOUNCE = 0.7;
			// bounce(弹力)
			FRICTION_THRESHOLD = 0.5;
			// friction(摩擦力)
			FRICTION_STATIC = 0.3;
			FRICTION_DYNAMIC_RATIO = 0.5;
			AppBuildModules();
			StartApp();
		}

		public function StartApp():void
		{
			this.stage.addEventListener(Event.ENTER_FRAME, this.RunApp);
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown);
			this.stage.addEventListener(KeyboardEvent.KEY_UP, this.onKeyUp);

			APP_KEY_TRIG = false;
			APP_KEY_PRESSED = false;
			APP_t0 = getTimer();
			APP_FPSBOX = this.gfx.CreateSprite("fpsBox", NinjaGraphicsSystem.LAYER_GUI);
			APP_FPSBOX.x = 0;
			APP_FPSBOX.y = 580;
		}

		public function onKeyDown(event:KeyboardEvent):void
		{
			if (APP_KEY_PRESSED == false)
			{
				APP_KEY_TRIG = true;
			}
			else
			{
				APP_KEY_TRIG = false;
			}
			APP_KEY_PRESSED = true;
		}

		public function onKeyUp(event:KeyboardEvent):void
		{
			APP_KEY_PRESSED = false;
		}

		public function RunApp(event:Event):void
		{
			var _local2:Number = APP_t0;
			APP_t0 = getTimer();
			var _local1 = APP_t0 - _local2;
			APP_FPSBOX.txt = "" + Math.ceil(_local1);
			this.input.Update();
			this.console.Update();
			TickApp();
		}

		public function CloseApp():void
		{
		}

		public var TickApp:Function;

		public function SetActiveProcess(func):void
		{
			TickApp = func;
		}

		public var CURRENT_APP_BUILD_STEP:int;

		public function AppBuildModules():void
		{
			CURRENT_APP_BUILD_STEP = -1;
			SetActiveProcess(AppBuildingModules);
		}

		public function AppBuildingModules():void
		{
			if (CURRENT_APP_BUILD_STEP == -1)
			{
				console = new ConsoleObject(16, 16, 600, 300);
				CURRENT_APP_BUILD_STEP++;
				this.console.Clear();
				this.console.AddLine("Building App Modules:");
				this.console.StartTab();
				this.console.AddLine("ConsoleObject built.");
				gui = new NinjaGUI();
				this.console.AddLine("NinjaGUI built.");
				this.gui.Display(NinjaGUI.GUI_LOADINGAPP);
			}
			else if (CURRENT_APP_BUILD_STEP == 0)
			{
				tiles = new TileMap(31, 23, APP_TILE_SCALE, APP_TILE_SCALE);
				CURRENT_APP_BUILD_STEP++;
				this.console.AddLine("TileMap built.");
				this.console.StartTab();
				this.console.AddLine("initing TileMapCells.");
			}
			else if (CURRENT_APP_BUILD_STEP == 1)
			{
				this.console.Append(".");
				if (!this.tiles.Building())
				{
					this.console.StopTab();
					CURRENT_APP_BUILD_STEP++;
				}
			}
			else if (CURRENT_APP_BUILD_STEP == 2)
			{
				objects = new ObjectManager();
				CURRENT_APP_BUILD_STEP++;
				this.console.AddLine("ObjectManager built.");
			}
			else if (CURRENT_APP_BUILD_STEP == 3)
			{
				userdata = new NinjaUserData();
				CURRENT_APP_BUILD_STEP++;
				this.console.AddLine("NinjaUserData built.");
			}
			else if (CURRENT_APP_BUILD_STEP == 4)
			{
				game = new NinjaGame();
				CURRENT_APP_BUILD_STEP++;
				this.console.AddLine("NinjaGame built.");
			}
			else if (CURRENT_APP_BUILD_STEP == 5)
			{
				editor = new NinjaEditor();
				CURRENT_APP_BUILD_STEP++;
				this.console.AddLine("NinjaEditor built.");
			}
			else if (CURRENT_APP_BUILD_STEP == 6)
			{
				gamedata = new NinjaData();
				CURRENT_APP_BUILD_STEP++;
				this.console.AddLine("NinjaData built.");
			}
			else
			{
				this.console.StopTab();
				App_LoadMainMenu();
			}
		}

		public function App_LoadMainMenu():void
		{
			// var _local1 = new Sound();
			// _local1.stop();
			App_StartLoadingMenuDemo();
			App_StartMainMenu();
		}

		public function App_StartMainMenu():void
		{
			APP_PRE_QUIT = false;
			App_PlayerDeathEvent = App_PlayerDeathEvent_Normal;
			App_LevelPassedEvent = App_PlayerDeathEvent_Normal;
			this.gui.HideInGame();
			this.gui.Display(NinjaGUI.GUI_MAINMENU);
			SetActiveProcess(App_Tick_MainMenu);
		}

		public function GUIEvent_MainMenu_Help():void
		{
			this.console.Show();
			this.game.StopDemoPlayback();
			// var _local1 = new Sound();
			// _local1.stop();
			App_StartHelp();
		}

		public function GUIEvent_MainMenu_Story():void
		{
			this.console.Hide();
			this.gui.Display(NinjaGUI.GUI_STORY);
		}

		public function GUIEvent_MainMenu_Quit():void
		{
			this.console.Hide();
			App_StartQuit();
		}

		public function GUIEvent_MainMenu_Credits():void
		{
			this.console.Hide();
			App_StartCredits();
		}

		public function GUIEvent_MainMenu_PlayGame():void
		{
			App_StartPlayMenu();
		}

		public function GUIEvent_MainMenu_Configure():void
		{
			App_StartConfigMenu();
		}

		public function GUIEvent_MainMenu_ContinueGame():void
		{
			App_StartContinue();
		}

		public var App_UpdateMainMenu:Function;

		public function App_Tick_MainMenu():*
		{
			if (APP_PRE_QUIT)
			{
				if (APP_KEY_TRIG && (KeyManager.isDown(89)))
				{
					APP_KEY_TRIG = false;
					App_Quit();
					return(undefined);
				}
				else if (APP_KEY_TRIG && (KeyManager.isDown(78)))
				{
					APP_KEY_TRIG = false;
					App_StartMainMenu();
				}
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(72)))
			{
				APP_KEY_TRIG = false;
				GUIEvent_MainMenu_Help();
				return(undefined);
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(83)))
			{
				APP_KEY_TRIG = false;
				GUIEvent_MainMenu_Story();
				return(undefined);
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(81)))
			{
				APP_KEY_TRIG = false;
				GUIEvent_MainMenu_Quit();
				return(undefined);
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(78)))
			{
				APP_KEY_TRIG = false;
				GUIEvent_MainMenu_PlayGame();
				return(undefined);
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(67)))
			{
				APP_KEY_TRIG = false;
				GUIEvent_MainMenu_Configure();
				return(undefined);
			}
			else if ((APP_KEY_TRIG && (KeyManager.isDown(192))) || (APP_KEY_TRIG && (KeyManager.isDown(220))))
			{
				APP_KEY_TRIG = false;
				this.game.StopDemoPlayback();
				App_StartNewGame_Debug();
				return(undefined);
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(101)))
			{
				APP_KEY_TRIG = false;
				GUIEvent_MainMenu_ContinueGame();
				return(undefined);
			}
			else if (APP_KEY_TRIG && KeyManager.isDown(96))
			{
				APP_KEY_TRIG = false;
				GUIEvent_MainMenu_Credits();
				return(undefined);
			}
			App_UpdateMainMenu();
		}

		public function App_StartLoadingMenuDemo():void
		{
			App_PlayerDeathEvent = App_PlayerDeathEvent_Normal;
			App_LevelPassedEvent = App_PlayerDeathEvent_Normal;
			App_UpdateMainMenu = App_UpdateMainMenu_LoadDemo;
			this.game.InitNewGame();
			var _local1 = gamedata.GetCurrentMenuDemoID();
			App_LoadMenuDemo(_local1);
			gamedata.IncrementCurrentMenuDemo();
			this.console.Show();
		}

		public function App_UpdateMainMenu_LoadDemo():void
		{
			if (!App_LoadingMenuDemo())
			{
				this.console.Hide();
				this.game.InitNewLevel();
				this.game.StartDemoPlayback();
				App_UpdateMainMenu = App_UpdateMainMenu_TickDemo;
				App_ResetGameTime();
			}
		}

		public function App_UpdateMainMenu_TickDemo():void
		{
			if ((this.game.GetDemoTickCount() - this.game.GetTime()) < -100)
			{
				// var _local1 = new Sound();
				// _local1.stop();
				this.game.StopDemoPlayback();
				App_StartLoadingMenuDemo();
				return;
			}
			else
			{
				App_UpdateGame_Demo();
			}
		}

		public function App_StartPlayMenu():void
		{
			this.gui.Display(NinjaGUI.GUI_PLAYMENU);
			SetActiveProcess(App_TickPlayMenu);
			var ep0 = this.userdata.GetEpisodeReached(0);
			var ep1 = this.userdata.GetEpisodeReached(1);
			var ep2 = this.userdata.GetEpisodeReached(2);
			var menuMC = this.gui.GetCurrentMC();
			var i = 0;
			while (i < 10)
			{
				// var but = eval(menuMC + ".playmenu" + ".e" + i);
				var but = menuMC["playmenu"]["e" + i];
				if (i <= ep0)
				{
					but.num.text = "0" + i;
					but.onRollOver = function():void
					{
						this.gfx.gotoAndStop(2);
					};
					but.onRollOut = function():void
					{
						this.gfx.gotoAndStop(1);
					};
				}
				else
				{
					but.num.text = "";
					but.enabled = false;
				}
				i++;
			}
			var i = 10;
			while (i < 20)
			{
				// var but = eval(((menuMC + ".playmenu") + ".e") + i);
				var but = menuMC + ["playmenu"]["e" + i];
				if (i <= ep1)
				{
					but.num.text = "" + i;
					but.onRollOver = function():void
					{
						this.gfx.gotoAndStop(2);
					};
					but.onRollOut = function():void
					{
						this.gfx.gotoAndStop(1);
					};
				}
				else
				{
					but.num.text = "";
					but.enabled = false;
				}
				i++;
			}
			var i = 20;
			while (i < 30)
			{
				var but = menuMC["playmenu"]["e" + i];
				if (i <= ep2)
				{
					but.num.text = "" + i;
					but.onRollOver = function():void
					{
						this.gfx.gotoAndStop(2);
					}
					but.onRollOut = function():void
					{
						this.gfx.gotoAndStop(1);
					}
				}
				else
				{
					but.num.text = "";
					but.enabled = false;
				}
				i++;
			}
		}

		public function App_TickPlayMenu():void
		{
			App_UpdateMainMenu();
		}

		public function App_EpisodeButtonClicked(txt):void
		{
			if (txt == "")
			{
				return;
			}
			else
			{
				var _local1 = Number(txt);
				if (!userdata.ValidateEpisodeReached(_local1))
				{
					gamedata.ResetEpisode();
					this.console.AddLine("Access Denied: " + _local1);
				}
				else if (gamedata.LoadEpisodeNum(_local1))
				{
					// var _local2 = new Sound();
					// _local2.stop();
					App_StartNewGame();
				}
			}
		}

		public function App_StartContinue():void
		{
			this.gui.Display(NinjaGUI.GUI_CONTINUE);
			SetActiveProcess(App_TickContinue);
		}

		public function App_TickContinue():void
		{
			App_UpdateMainMenu();
		}

		public function App_ReadInputContinueCode():void
		{
			var _local2 = this.gui.GetCurrentMC().continuemenu;
			var _local1 = _local2.txt;
			App_SubmitContinueCode(_local1);
		}

		public function App_SubmitContinueCode(code):void
		{
			if (gamedata.LoadEpisode(code))
			{
				// var _local1 = new Sound();
				// _local1.stop();
				App_StartNewGame();
			}
			else
			{
				gamedata.ResetEpisode();
				this.console.AddLine("Invalid Episode Code.");
				App_StartMainMenu();
			}
		}

		public function App_StartCredits():void
		{
			var _local1 = Math.random();
			if (_local1 < 0.25)
			{
				this.gui.Display(NinjaGUI.GUI_GOODBYE4);
			}
			else if (_local1 < 0.5)
			{
				this.gui.Display(NinjaGUI.GUI_GOODBYE3);
			}
			else if (_local1 < 0.75)
			{
				this.gui.Display(NinjaGUI.GUI_GOODBYE2);
			}
			else
			{
				this.gui.Display(NinjaGUI.GUI_GOODBYE1);
			}
			SetActiveProcess(App_TickCredits);
		}

		public function App_TickCredits():void
		{
			if (APP_KEY_TRIG)
			{
				APP_KEY_TRIG = false;
				App_ResetGameTime();
				App_StartMainMenu();
			}
		}

		public function App_StartConfigMenu():void
		{
			this.gui.Display(NinjaGUI.GUI_CONFIGURE);
			SetActiveProcess(App_TickConfigure);
			var menuMC = this.gui.GetCurrentMC();
			// var jbut = eval(menuMC + ".configmenu.jbut");
			// var lbut = eval(menuMC + ".configmenu.lbut");
			// var rbut = eval(menuMC + ".configmenu.rbut");
			var jbut = menuMC["configmenu"]["jbut"];
			var lbut = menuMC["configmenu"]["lbut"];
			var rbut = menuMC["configmenu"]["rbut"];
			jbut.keyname.text = App_GetKeyName(this.userdata.GetJumpKey());
			lbut.keyname.text = App_GetKeyName(this.userdata.GetLeftKey());
			rbut.keyname.text = App_GetKeyName(this.userdata.GetRightKey());
		}

		public function App_TickConfigure():void
		{
			App_UpdateMainMenu();
		}

		public function App_SubmitKeyConfig(kbut, ktype, kcode):void
		{
			var _local1 = kcode;
			var _local2 = ktype;
			APP_KEY_TRIG = false;
			this.gui.HideTextMenu();
			// Key.removeListener(kbut);
			this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, kbut.onKeyDown);
			this.stage.removeEventListener(KeyboardEvent.KEY_UP, kbut.onKeyUp);
			if (_local1 != 81)
			{
				kbut.keyname.text = App_GetKeyName(_local1);
				if (_local2 == 0)
				{
					this.userdata.SetJumpKey(_local1);
				}
				else if (_local2 == -1)
				{
					this.userdata.SetLeftKey(_local1);
				}
				else if (_local2 == 1)
				{
					this.userdata.SetRightKey(_local1);
					return;
				}
			}
		}

		public function App_FocusKeyConfig(kbut):void
		{
			this.gui.DisplayTextBar(NinjaGUI.GUI_KEYCONFIG);
			// Key.addListener(kbut);
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, kbut.onKeyDown);
			this.stage.addEventListener(KeyboardEvent.KEY_UP, kbut.onKeyUp);
		}

		public function App_ResetKeyConfig():void
		{
			this.userdata.SetJumpKey(16);
			this.userdata.SetLeftKey(37);
			this.userdata.SetRightKey(39);
			var menuMC = this.gui.GetCurrentMC();
			// var jbut = eval(menuMC + ".configmenu.jbut");
			// var lbut = eval(menuMC + ".configmenu.lbut");
			// var rbut = eval(menuMC + ".configmenu.rbut");
			var jbut = menuMC["configmenu"]["jbut"];
			var lbut = menuMC["configmenu"]["lbut"];
			var rbut = menuMC["configmenu"]["rbut"];
			jbut.keyname.text = App_GetKeyName(this.userdata.GetJumpKey());
			lbut.keyname.text = App_GetKeyName(this.userdata.GetLeftKey());
			rbut.keyname.text = App_GetKeyName(this.userdata.GetRightKey());
		}

		public function App_GetKeyName(kcode):String
		{
			var _local2 = kcode;
			var _local1 = "";
			if (_local2 == 37)
			{
				_local1 = _local1 + "L arrow";
			}
			else if (_local2 == 39)
			{
				_local1 = _local1 + "R arrow";
			}
			else if (_local2 == 38)
			{
				_local1 = _local1 + "U arrow";
			}
			else if (_local2 == 40)
			{
				_local1 = _local1 + "D arrow";
			}
			else if (_local2 == 32)
			{
				_local1 = _local1 + "space";
			}
			else if (_local2 == 16)
			{
				_local1 = _local1 + "shift";
			}
			else if (_local2 == 17)
			{
				_local1 = _local1 + "ctrl";
			}
			else if (_local2 == 18)
			{
				_local1 = _local1 + "alt";
			}
			else if (_local2 == 13)
			{
				_local1 = _local1 + "enter";
			}
			else if (_local2 == 9)
			{
				_local1 = _local1 + "tab";
			}
			else
			{
				_local1 = _local1 + String.fromCharCode(_local2);
			}
			return(_local1);
			return(_local2);
		}

		public function App_StartHelp():void
		{
			this.gui.Display(NinjaGUI.GUI_HELP);
			var _local1 = this.gui.GetCurrentMC().helpmenu;
			_local1.keyL.visible = false;
			_local1.keyR.visible = false;
			_local1.keyJ.visible = false;
			SetActiveProcess(App_Tick_Help);
			App_UpdateHelp = App_UpdateHelp_LoadLevel;
			APP_HELPLEVEL_LOADED = false;
			App_BeginLoadMenuDemo(gamedata.GetHelpLevelData(), "");
		}

		public function App_UpdateHelp_LoadLevel():void
		{
			if (!App_LoadingMenuDemo())
			{
				APP_HELPLEVEL_LOADED = true;
				App_StartHelpDemo(NinjaData.HELPDEMO_WELCOME);
			}
		}

		public function App_StartHelpDemo(demoID):void
		{
			if (APP_HELPLEVEL_LOADED)
			{
				// var _local1 = new Sound();
				// _local1.stop();
				this.console.Show();
				App_LoadHelpDemo(demoID);
				App_UpdateHelp = App_UpdateHelp_LoadDemo;
			}
		}

		public function App_ContinueHelpDemo():void
		{
			this.console.Show();
			// var _local1 = new Sound();
			// _local1.stop();
			App_ResetHelpDemo();
			App_UpdateHelp = App_UpdateHelp_ResetDemo;
		}

		public function App_PlayHelpDemo(str):void
		{
			// var _local2 = new Sound();
			// _local2.stop();
			App_PlayerDeathEvent = App_PlayerDeathEvent_Normal;
			App_LevelPassedEvent = App_PlayerDeathEvent_Normal;
			this.game.InitNewGame();
			App_ResetGameTime();
			this.gui.ResetPlayerTime();
			this.game.StopDemoPlayback();
			this.game.LoadDemo(str);
			this.game.StartDemoPlayback();
			App_UpdateHelp = App_UpdateHelp_TickDemo;
			var _local1 = this.gui.GetCurrentMC().helpmenu;
			_local1.keyL.visible = true;
			_local1.keyR.visible = true;
			_local1.keyJ.visible = true;
			_local1.keyL.gotoAndStop(2);
			_local1.keyR.gotoAndStop(2);
			_local1.keyJ.gotoAndStop(2);
		}

		public function App_Tick_Help():void
		{
			if (KeyManager.isDown(49))
			{
				App_StartHelpDemo(NinjaData.HELPDEMO_JUMPING1);
			}
			else if (KeyManager.isDown(50))
			{
				App_StartHelpDemo(NinjaData.HELPDEMO_JUMPING2);
			}
			else if (KeyManager.isDown(51))
			{
				App_StartHelpDemo(NinjaData.HELPDEMO_MOVEMENT);
			}
			if (APP_KEY_TRIG && (KeyManager.isDown(77)))
			{
				APP_KEY_TRIG = false;
				App_LoadMainMenu();
			}
			else
			{
				App_UpdateHelp();
			}
		}

		public function App_UpdateHelp_LoadDemo():void
		{
			if (!App_LoadingHelpDemo())
			{
				this.console.Hide();
				App_PlayHelpDemo(gamedata.GetCurrentHelpDemoData());
			}
		}

		public function App_UpdateHelp_ResetDemo():void
		{
			if (!App_ResettingHelpDemo())
			{
				this.console.Hide();
				App_PlayHelpDemo(gamedata.GetCurrentHelpDemoData());
			}
		}

		public function App_UpdateHelp_TickDemo():void
		{
			if ((this.game.GetDemoTickCount() - this.game.GetTime()) < -60)
			{
				gamedata.IncrementHelpDemoReel();
				App_ContinueHelpDemo();
				return;
			}
			else
			{
				App_UpdateGame_Demo();
				var _local1 = this.gui.GetCurrentMC().helpmenu;
				var _local2 = PlayerObject.player.inputList;
				if (_local2[NinjaGame.PINPUT_R])
				{
					_local1.keyR.gotoAndStop(1);
				}
				else
				{
					_local1.keyR.gotoAndStop(2);
				}
				if (_local2[NinjaGame.PINPUT_L])
				{
					_local1.keyL.gotoAndStop(1);
				}
				else
				{
					_local1.keyL.gotoAndStop(2);
				}
				if (_local2[NinjaGame.PINPUT_J])
				{
					_local1.keyJ.gotoAndStop(1);
				}
				else
				{
					_local1.keyJ.gotoAndStop(2);
				}
			}
		}

		public function App_StartQuit():void
		{
			this.gui.Display(NinjaGUI.GUI_CONFIRMQUIT);
			APP_PRE_QUIT = true;
		}

		public function App_Quit():void
		{
			// var _local2 = new Sound();
			// _local2.stop();
			var _local1 = Math.random();
			if (_local1 < 0.25)
			{
				this.gui.Display(NinjaGUI.GUI_GOODBYE1);
			}
			else if (_local1 < 0.5)
			{
				this.gui.Display(NinjaGUI.GUI_GOODBYE2);
			}
			else if (_local1 < 0.75)
			{
				this.gui.Display(NinjaGUI.GUI_GOODBYE3);
			}
			else
			{
				this.gui.Display(NinjaGUI.GUI_GOODBYE4);
			}
			this.console.mc.visible = false;
			SetActiveProcess(App_TickGoodbye);
		}

		public function App_TickGoodbye():void
		{
			if (APP_KEY_TRIG)
			{
				SetActiveProcess(App_Cleanup);
			}
		}

		public function App_Cleanup():void
		{
			CloseApp();
			// fscommand("quit");
		}

		public function App_PlayerDeathEvent_Normal():void
		{
			this.objects.IdleObjectsAfterDeath();
		}

		public function App_PlayerDeathEvent_Debug():void
		{
		}

		public function App_PlayerDeathEvent_Demo():void
		{
			this.objects.IdleObjectsAfterDeath();
		}

		public function App_LevelPassedEvent_Normal():void
		{
			this.console.AddLine("level completed.");
			this.objects.IdleObjectsAfterDeath();
			var _local1 = gamedata.IncrementCurrentLevel();
			if (_local1)
			{
				this.game.InitNewLevel();
				App_StartPostLevelPause();
			}
			else
			{
				App_StartPostLevelPause_EpVictory();
			}
		}

		public function App_LevelPassedEvent_Debug():void
		{
			this.console.AddLine("level completed.");
		}

		public function App_LevelPassedEvent_Demo():void
		{
		}

		public function App_StartBeatGame():void
		{
			this.gui.Display(NinjaGUI.GUI_VICTORY);
			this.gui.DisplayTextBar(NinjaGUI.GUI_BEATGAME);
			SetActiveProcess(App_TickBeatGame);
		}

		public function App_TickBeatGame():void
		{
			App_UpdateGame_Demo();
			if (APP_KEY_TRIG && (KeyManager.isDown(32)))
			{
				App_LoadMainMenu();
			}
		}

		public function App_StartPassedEpisode(num):void
		{
			this.gui.HideAll();
			this.gui.Display(NinjaGUI.GUI_VICTORY);
			this.gui.DisplayTextBar(NinjaGUI.GUI_PASSEDEPISODE);
			var _local1 = num - 1;
			this.gui.AppendToTextBar(("episode [" + _local1) + "] complete!!  [spacebar] to continue, [Q] for mainmenu");
			gamedata.LoadEpisodeNum(num);
			SetActiveProcess(App_TickPassedEpisode);
		}

		public function App_TickPassedEpisode():void
		{
			App_UpdateGame_Demo();
			if (APP_KEY_TRIG && (KeyManager.isDown(32)))
			{
				this.gui.HideInGame();
				App_StartNewGame();
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(81)))
			{
				APP_KEY_TRIG = false;
				this.gui.HideInGame();
				App_LoadMainMenu();
			}
		}

		public function App_StartNewGame():void
		{
			this.console.Show();
			// var _local1 = new Sound();
			// _local1.stop();
			this.game.InitNewGame();
			App_PlayerDeathEvent = App_PlayerDeathEvent_Normal;
			App_LevelPassedEvent = App_LevelPassedEvent_Normal;
			App_LoadLevel(0, App_StartPreLevelPause);
		}

		public var APP_WAITING_TO_RETRY:Boolean;

		public function App_StartPreLevelPause():void
		{
			APP_WAITING_TO_RETRY = false;
			this.console.Hide();
			this.gui.Display(NinjaGUI.GUI_PRELEVEL);
			this.gui.ShowInGame();
			this.game.InitRetryLevel();
			this.gui.ResetPlayerTime();
			this.gui.DrawLevelName(gamedata.GetCurrentLevelName());
			SetActiveProcess(App_Tick_PreLevelPause);
		}

		public function App_Tick_PreLevelPause():void
		{
			this.game.FillPlayerTime();
			if (KeyManager.isDown(32))
			{
				App_PlayGame();
			}
			if (KeyManager.isDown(13))
			{
				if (Keyboard.capsLock)
				{
					if (KeyManager.isDown(16))
					{
						if (KeyManager.isDown(90))
						{
							App_ResetGameTime();
							App_LevelPassedEvent();
						}
					}
				}
			}
		}

		public var APP_VOLUNTARY_SUICIDE:Boolean;

		public function App_PlayGame():void
		{
			this.gui.HideAll();
			this.game.StopDemoPlayback();
			this.game.StopRecordingDemo();
			this.game.InitRetryLevel();
			this.game.StartRecordingDemo();
			App_ResetGameTime();
			APP_VOLUNTARY_SUICIDE = false;
			App_PlayerDeathEvent = App_PlayerDeathEvent_Normal;
			App_LevelPassedEvent = App_LevelPassedEvent_Normal;
			SetActiveProcess(App_Tick_RunningGame);
		}

		public function App_UnpauseGame():void
		{
			this.gui.HideAll();
			App_ResetGameTime();
			SetActiveProcess(App_Tick_RunningGame);
		}

		public function App_Tick_RunningGame():void
		{
			var _local1 = APP_KEY_TRIG && (!PlayerObject.player.isDead);
			if ((_local1 && (KeyManager.isDown(80))) || (_local1 && (KeyManager.isDown(27))))
			{
				APP_KEY_TRIG = false;
				App_PauseGame();
			}
			else
			{
				if (APP_KEY_TRIG && (KeyManager.isDown(75)))
				{
					APP_KEY_TRIG = false;
					APP_VOLUNTARY_SUICIDE = true;
					if ((this.game.GetTime() % 2) == 0)
					{
						this.game.KillPlayer(NinjaGame.KILLTYPE_EXPLOSIVE, (Math.random() * 10) - 5, (-Math.random()) * 6, PlayerObject.player.pos.x, PlayerObject.player.pos.y, PlayerObject.player);
					}
					else
					{
						this.game.KillPlayer(NinjaGame.KILLTYPE_HARDBULLET, (Math.random() * 10) - 5, (-Math.random()) * 6, PlayerObject.player.pos.x, PlayerObject.player.pos.y, PlayerObject.player);
					}
				}
				if (PlayerObject.player.isDead)
				{
					App_UpdateGame_Demo();
					if (APP_WAITING_TO_RETRY)
					{
						if (KeyManager.isDown(32))
						{
							APP_KEY_TRIG = false;
							// var _local2 = new Sound();
							// _local2.stop();
							App_ResetObjects(App_StartPreLevelPause);
						}
						else if (!APP_VOLUNTARY_SUICIDE)
						{
							if (KeyManager.isDown(82))
							{
								APP_KEY_TRIG = false;
								// var _local2 = new Sound();
								// _local2.stop();
								APP_REPLAY_DATA = this.game.DumpDemoData();
								App_StartInGameDemo();
							}
						}
					}
					else if (40 < (this.game.GetTime() - PlayerObject.player.timeOfDeath))
					{
						this.game.StopRecordingDemo();
						App_StartRetryMenu(APP_VOLUNTARY_SUICIDE);
					}
				}
				else
				{
					App_UpdateGame();
					if (this.game.playerCurTime <= 0)
					{
						this.game.KillPlayer(NinjaGame.KILLTYPE_FALL, 0, 0, PlayerObject.player.pos.x, PlayerObject.player.pos.y, PlayerObject.player);
						App_StartGameOver();
					}
				}
			}
		}

		public function App_UpdateGame():void
		{
			var _local2 = APP_GAMETIME_t0;
			APP_GAMETIME_t0 = APP_t0;
			var _local1 = (APP_GAMETIME_t0 - _local2) + APP_GAMETIME_REMAINDER;
			var _local3 = _local1;
			while (this.APP_GAMETIME_TICKLEN < _local1)
			{
				_local1 = _local1 - this.APP_GAMETIME_TICKLEN;
				this.game.Tick();
				this.game.playerCurTime--;
			}
			APP_GAMETIME_REMAINDER = _local1;
			if (_local1 < _local3)
			{
				this.game.Draw();
				this.game.DrawPlayerTime();
			}
		}

		public function App_UpdateGame_Demo():void
		{
			var _local2 = APP_GAMETIME_t0;
			APP_GAMETIME_t0 = APP_t0;
			var _local1 = (APP_GAMETIME_t0 - _local2) + APP_GAMETIME_REMAINDER;
			var _local3 = _local1;
			while (this.APP_GAMETIME_TICKLEN < _local1)
			{
				_local1 = _local1 - this.APP_GAMETIME_TICKLEN;
				this.game.Tick();
			}
			APP_GAMETIME_REMAINDER = _local1;
			if (_local1 < _local3)
			{
				this.game.Draw();
			}
		}

		public function App_ResetGameTime():void
		{
			APP_GAMETIME_t0 = APP_t0;
			APP_GAMETIME_REMAINDER = 0;
		}

		public function App_PauseGame():void
		{
			this.gui.Display(NinjaGUI.GUI_PAUSE);
			SetActiveProcess(App_Tick_InGamePause);
		}

		public function App_Tick_InGamePause():void
		{
			if ((APP_KEY_TRIG && (KeyManager.isDown(80))) || (APP_KEY_TRIG && (KeyManager.isDown(27))))
			{
				APP_KEY_TRIG = false;
				App_UnpauseGame();
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(81)))
			{
				APP_KEY_TRIG = false;
				App_LoadMainMenu();
			}
			else
			{
				APP_KEY_TRIG = false;
			}
		}

		public function App_StartPostLevelPause():void
		{
			this.gui.Display(NinjaGUI.GUI_POSTLEVEL);
			SetActiveProcess(App_Tick_PostLevelPause);
		}

		public var APP_VICTORY:Boolean;

		public function App_Tick_PostLevelPause():void
		{
			if (KeyManager.isDown(32))
			{
				APP_VICTORY = false;
				this.console.Show();
				// var _local1 = new Sound();
				// _local1.stop();
				this.gui.HideInGame();
				App_LoadLevel(gamedata.GetCurrentLevelID(), App_StartPreLevelPause);
			}
			else
			{
				App_UpdateGame_Demo();
			}
		}

		public function App_StartPostLevelPause_EpVictory():void
		{
			this.gui.Display(NinjaGUI.GUI_POSTLEVEL);
			SetActiveProcess(App_Tick_PostLevelPause_EpVictory);
		}

		public function App_Tick_PostLevelPause_EpVictory():void
		{
			if (KeyManager.isDown(32))
			{
				APP_KEY_TRIG = false;
				var _local1 = gamedata.GetNextEpisodeNum();
				if (_local1 < 0)
				{
					this.console.AddLine("journey completed.");
					App_StartBeatGame();
				}
				else
				{
					this.userdata.NotifyEpisodeReached(_local1);
					this.console.AddLine("episode completed. #: " + _local1);
					App_StartPassedEpisode(_local1);
				}
			}
			else
			{
				App_UpdateGame_Demo();
			}
		}

		public function App_StartRetryMenu(suicide):void
		{
			if (suicide)
			{
				this.gui.Display(NinjaGUI.GUI_RETRYLEVEL_SUICIDE);
			}
			else
			{
				this.gui.Display(NinjaGUI.GUI_RETRYLEVEL);
			}
			APP_WAITING_TO_RETRY = true;
		}

		public function App_StartGameOver():void
		{
			this.gui.Display(NinjaGUI.GUI_DEFEAT);
			this.gui.DisplayTextBar(NinjaGUI.GUI_GAMEOVER);
			SetActiveProcess(App_Tick_GameOver);
		}

		public function App_Tick_GameOver():void
		{
			if (KeyManager.isDown(32))
			{
				App_LoadMainMenu();
			}
			else
			{
				App_UpdateGame_Demo();
			}
		}

		public function App_StartInGameDemo():void
		{
			App_PlayerDeathEvent = App_PlayerDeathEvent_Demo;
			App_LevelPassedEvent = App_LevelPassedEvent_Demo;
			App_ResetGameTime();
			this.game.InitRetryLevel();
			this.game.StopDemoPlayback();
			this.game.LoadDemo(APP_REPLAY_DATA);
			this.game.StartDemoPlayback();
			App_ResetObjects(App_TickInGameDemo);
			this.gui.Display(NinjaGUI.GUI_INGAMEDEMO);
		}

		public function App_TickInGameDemo():void
		{
			if (KeyManager.isDown(32))
			{
				APP_KEY_TRIG = false;
				// var _local1 = new Sound();
				// _local1.stop();
				App_ResetObjects(App_StartPreLevelPause);
			}
			else if ((this.game.GetDemoTickCount() - this.game.GetTime()) < -120)
			{
				// var _local1 = new Sound();
				// _local1.stop();
				App_StartInGameDemo();
			}
			else
			{
				App_UpdateGame_Demo();
			}
		}

		public var APP_REPLAY_DATA = "";
		public var APP_VISTORY = false;
		public var APP_GAMETIME_t0 = 0;
		public var APP_GAMETIME_REMAINDER = 0;

		public function App_StartNewGame_Debug():void
		{
			this.game.InitNewGame();
			App_StartDebugMenu();
		}

		public function App_StartDebugMenu():void
		{
			App_PlayerDeathEvent = App_PlayerDeathEvent_Debug;
			App_LevelPassedEvent = App_LevelPassedEvent_Debug;
			this.gui.Display(NinjaGUI.GUI_DEBUGMENU);
			this.console.Show();
			this.gui.ShowTxt();
			this.gui.SetTxt(NinjaGUI.TXTBOX_TOP, this.game.DumpLevelData());
			this.gui.SetTxt(NinjaGUI.TXTBOX_BOTTOM, this.game.DumpDemoData());
			SetActiveProcess(App_Tick_DebugMenu);
		}

		public function App_Tick_DebugMenu():void
		{
			if (APP_KEY_TRIG && (KeyManager.isDown(49)))
			{
				APP_KEY_TRIG = false;
				this.game.StopRecordingDemo();
				this.game.InitNewGame();
				this.game.StartRecordingDemo();
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(50)))
			{
				APP_KEY_TRIG = false;
				this.game.StopRecordingDemo();
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(87)))
			{
				APP_KEY_TRIG = false;
				this.game.StopDemoPlayback();
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(81)))
			{
				APP_KEY_TRIG = false;
				this.game.InitNewGame();
				App_ResetObjects(App_StartDemoPlayback_Debug);
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(51)))
			{
				APP_KEY_TRIG = false;
				var _local1 = this.gui.GetTxt(NinjaGUI.TXTBOX_BOTTOM);
				this.game.LoadDemo(_local1);
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(52)))
			{
				APP_KEY_TRIG = false;
				var _local1 = this.game.DumpDemoData();
				System.setClipboard(_local1);
				this.gui.ShowTxt();
				this.gui.SetTxt(NinjaGUI.TXTBOX_BOTTOM, _local1);
			}
			if (APP_KEY_TRIG && (KeyManager.isDown(77)))
			{
				APP_KEY_TRIG = false;
				this.gui.HideTxt();
				App_LoadMainMenu();
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(74)))
			{
				APP_KEY_TRIG = false;
				App_LoadLevel_Raw(gamedata.GetBlankMap(), App_StartDebugMenu);
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(80)))
			{
				APP_KEY_TRIG = false;
				this.gui.HideTxt();
				this.console.Hide();
				App_PlayGame_Debug();
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(69)))
			{
				APP_KEY_TRIG = false;
				this.gui.HideTxt();
				this.console.Hide();
				App_StartEditor();
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(82)))
			{
				APP_KEY_TRIG = false;
				App_ResetObjects(App_StartDebugMenu);
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(76)))
			{
				APP_KEY_TRIG = false;
				var _local1 = this.gui.GetTxt(NinjaGUI.TXTBOX_TOP);
				App_LoadLevel_Raw(_local1, App_StartDebugMenu);
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(83)))
			{
				APP_KEY_TRIG = false;
				var _local1 = this.game.DumpLevelData();
				System.setClipboard(_local1);
				this.gui.ShowTxt();
				this.gui.SetTxt(NinjaGUI.TXTBOX_TOP, _local1);
			}
		}

		public function App_StartDemoPlayback_Debug():void
		{
			this.game.InitNewGame();
			this.game.StartDemoPlayback();
			App_StartDebugMenu();
		}

		public function App_PlayGame_Debug():void
		{
			this.gui.HideAll();
			App_ResetGameTime();
			SetActiveProcess(App_Tick_RunningGame_Debug);
		}

		public function App_Tick_RunningGame_Debug():void
		{
			if (KeyManager.isDown(192) || (KeyManager.isDown(220)))
			{
				APP_KEY_TRIG = false;
				App_StartDebugMenu();
				// return(undefined);
			}
			if (Keyboard.capsLock)
			{
				if (!APP_DID_TICK_DEBUG)
				{
					App_ResetGameTime();
				}
				DebugUpdateGameCode();
				App_UpdateGame();
				APP_DID_TICK_DEBUG = true;
			}
			else if (this.input.MousePressed())
			{
				this.static_rend.Clear();
				if (KeyManager.isDown(8))
				{
					PlayerObject.player.raggy.Activate();
					PlayerObject.player.raggy.MimicMC(0, 0, PlayerObject.player.mc, PlayerObject.player.facingDir, PlayerObject.player.prevframe);
					PlayerObject.player.mc.visible = false;
					PlayerObject.player.raggy.Draw();
				}
				if (KeyManager.isDown(45))
				{
					PlayerObject.player.raggy.Deactivate();
					PlayerObject.player.mc.visible = true;
				}
				App_ResetGameTime();
				APP_GAMETIME_REMAINDER = this.APP_GAMETIME_TICKLEN + 1;
				DebugUpdateGameCode();
				App_UpdateGame();
				APP_DID_TICK_DEBUG = true;
			}
			else
			{
				APP_DID_TICK_DEBUG = false;
			}
		}

		public function App_StartEditor():void
		{
			this.gui.Display(NinjaGUI.GUI_TEMP_EDITOR);
			SetActiveProcess(App_TickEditor);
			this.editor.Start();
		}

		public function App_TickEditor():void
		{
			App_UpdateEditor();
		}

		public function App_UpdateEditor():void
		{
			this.debug_rend.Clear();
			this.static_rend.Clear();
			this.editor.Tick();
		}

		public var APP_DID_TICK_DEBUG:Boolean = false;

		public function App_LoadHelpDemo(demoID):void
		{
			gamedata.SetCurrentHelpDemo(demoID);
			var _local1 = gamedata.GetHelpDemoObjects();
			if (_local1 != null)
			{
				App_BeginLoadHelpDemo(_local1);
				return;
			}
		}

		public function App_BeginLoadHelpDemo(objStr):void
		{
			this.console.AddLine("Loading Objects");
			this.console.AddLine(".");
			this.game.InitLoadObjects(objStr);
		}

		public function App_LoadingHelpDemo():Boolean
		{
			if (!AppLoadingObjects())
			{
				return(false);
			}
			return(true);
		}

		public function App_ResetHelpDemo():void
		{
			this.console.AddLine("Resetting Objects");
			this.console.AddLine(".");
			this.game.InitReloadObjects();
		}

		public function App_ResettingHelpDemo():Boolean
		{
			if (!AppLoadingObjects())
			{
				return(false);
			}
			return(true);
		}

		public function App_LoadMenuDemo(demoID):void
		{
			var _local1 = gamedata.GetMenuDemoData(demoID);
			this.game.LoadDemo(_local1);
			var _local2 = gamedata.GetMenuDemoLevel(demoID);
			if (_local2 != null)
			{
				App_BeginLoadMenuDemo(_local2, _local1);
				return;
			}
		}

		public var APP_DONE_LOADING_MAP:Boolean;

		public function App_BeginLoadMenuDemo(levStr, demStr):void
		{
			this.console.AddLine("Loading Level:");
			this.console.StartTab();
			this.console.AddLine("Loading Map");
			this.console.AddLine(".");
			APP_DONE_LOADING_MAP = false;
			this.game.InitLoadLevel(levStr);
		}

		public function App_LoadingMenuDemo():Boolean
		{
			if (!APP_DONE_LOADING_MAP)
			{
				if (!AppLoadingMap())
				{
					this.console.AddLine("Loading Objects");
					this.console.AddLine(".");
					APP_DONE_LOADING_MAP = true;
				}
			}
			else if (!AppLoadingObjects())
			{
				this.console.StopTab();
				return(false);
			}
			return(true);
		}

		public function App_LoadLevel(levelID, callback):void
		{
			this.gui.Display(NinjaGUI.GUI_LOADINGLEVEL);
			var _local1 = gamedata.GetLevelData(levelID);
			if (_local1 != null)
			{
				App_BeginLoadLevel(_local1, callback);
				return;
			}
		}

		public function App_LoadLevel_Raw(str, callback):void
		{
			this.gui.Display(NinjaGUI.GUI_LOADINGLEVEL);
			App_BeginLoadLevel(str, callback);
		}

		public function App_ResetObjects(callback):void
		{
			this.gui.Display(NinjaGUI.GUI_RESETTINGLEVEL);
			App_BeginResetObjects(callback);
		}

		public function App_BeginLoadLevel(levStr, callback):void
		{
			LEVEL_LOADED_CALLBACK = callback;
			this.console.AddLine("Loading Level:");
			this.console.StartTab();
			this.console.AddLine("Loading Map");
			this.console.AddLine(".");
			APP_DONE_LOADING_MAP = false;
			this.game.InitLoadLevel(levStr);
			SetActiveProcess(AppLoadingLevel);
		}

		public function App_BeginResetObjects(callback):void
		{
			OBJECTS_LOADED_CALLBACK = callback;
			this.console.AddLine("Resetting Objects");
			this.console.AddLine(".");
			this.game.InitReloadObjects();
			SetActiveProcess(AppResettingObjects);
		}

		public function AppResettingObjects():void
		{
			if (!AppLoadingObjects())
			{
				OBJECTS_LOADED_CALLBACK();
			}
		}

		public function AppLoadingLevel():void
		{
			if (!APP_DONE_LOADING_MAP)
			{
				if (!AppLoadingMap())
				{
					this.console.AddLine("Loading Objects");
					this.console.AddLine(".");
					APP_DONE_LOADING_MAP = true;
				}
			}
			else if (!AppLoadingObjects())
			{
				this.console.StopTab();
				LEVEL_LOADED_CALLBACK();
			}
		}

		public function AppLoadingMap():Boolean
		{
			var _local1 = 18;
			while (_local1--)
			{
				this.console.Append(".");
				if (!this.game.LoadingMap())
				{
					return(false);
				}
			}
			this.console.Update();
			return(true);
			return(_local1);
		}

		public function AppLoadingObjects():Boolean
		{
			var _local1 = 1;
			while (_local1--)
			{
				this.console.Append(".");
				if (!this.game.LoadingObjects())
				{
					return(false);
				}
			}
			this.console.Update();
			return(true);
			return(_local1);
		}

		public var LEVEL_LOADED_CALLBACK = null;
		public var OBJECTS_LOADED_CALLBACK = null;
		public var DEMO_LOADED_CALLBACK = null;

		public function NormToRot_U(dx, dy):Number
		{
			var _local1 = Math.atan2(dy, dx) / (Math.PI / 180);
			return(_local1);
			return(_local1);
		}

		public function NormToRot(dx, dy):Number
		{
			var _local1 = Math.atan2(dy, dx) / (Math.PI / 180);
			return(_local1);
			return(_local1);
		}

		public function CreateMC(linkName, instanceName):DisplayObject
		{
			var _local2 = instanceName;
			var _local3 = linkName;
			var _local1 = this.gfx.GetNextDepth_Front();
			if (linkName == "EMPTY_MC")
			{
				var mc:MovieClip = new MovieClip();
				mc.name = instanceName;
				return mcBuffer.addChildAtDepth(mc, _local1);
			}
			else if (linkName == "TEXT_MC")
			{
				var txt:TextField = new TextField();
				txt.name = instanceName;
				txt.width = txt.height = 100;
				return mcBuffer.addChildAtDepth(txt, _local1);
			}
			else
			{
				var child:MovieClip = new (getDefinitionByName("linkName") as Class)() as MovieClip;
				child.name = _local2 + _local1;
				return mcBuffer.addChildAtDepth(child, _local1);
			}
			return null;
		}

		public function DestroyMC(mc):void
		{
			mc.swapDepths(1048000);
			mc.removeMovieClip();
		}

		public function DebugUpdateGameCode():void
		{
			var _local3 = this.input.getMousePos();
			if (PlayerObject.player.isDead)
			{
				if (KeyManager.isDown(16))
				{
					var v = this.input.getMouseDelta();
					PlayerObject.player.raggy.Shove(v.x * 0.1, v.y * 0.1);
				}
				if (APP_KEY_TRIG && (KeyManager.isDown(32)))
				{
					APP_KEY_TRIG = false;
					if (PlayerObject.player.raggy.exploded == false)
					{
						PlayerObject.player.raggy.Explode();
					}
					else
					{
						PlayerObject.player.raggy.Unexplode();
					}
				}
				if (KeyManager.isDown(9))
				{
					PlayerObject.player.raggy.pList.h0.pos.x = (PlayerObject.player.raggy.pList.h0.oldpos.x = _local3.x);
					PlayerObject.player.raggy.pList.h0.pos.y = (PlayerObject.player.raggy.pList.h0.oldpos.y = _local3.y);
				}
				if (APP_KEY_TRIG && (KeyManager.isDown(13)))
				{
					APP_KEY_TRIG = false;
					PlayerObject.player.pos.copy(_local3);
					PlayerObject.player.oldpos.copy(_local3);
					PlayerObject.player.Stand();
				}
			}
			else if (APP_KEY_TRIG && (KeyManager.isDown(13)))
			{
				APP_KEY_TRIG = false;
				var _local2 = PlayerObject.player.pos.x - _local3.x;
				var _local1 = PlayerObject.player.pos.y - _local3.y;
				var len = Math.sqrt((_local2 * _local2) + (_local1 * _local1));
				if (len == 0)
				{
					this.game.KillPlayer(NinjaGame.KILLTYPE_SOFTBULLET, _local2 * 10, _local1 * 10, PlayerObject.player.pos.x, PlayerObject.player.pos.y, null);
				}
				else
				{
					_local2 = _local2 / len;
					_local1 = _local1 / len;
					if (KeyManager.isDown(32))
					{
						this.game.KillPlayer(NinjaGame.KILLTYPE_EXPLOSIVE, _local2 * 10, _local1 * 10, PlayerObject.player.pos.x, PlayerObject.player.pos.y, null);
					}
					else
					{
						this.game.KillPlayer(NinjaGame.KILLTYPE_SOFTBULLET, _local2 * 10, _local1 * 10, PlayerObject.player.pos.x, PlayerObject.player.pos.y, null);
					}
				}
			}
		}

		public function CollideAABBvsTileMap(box):void
		{
			var _local2 = box;
			var pos = _local2.pos;
			var _local1 = this.tiles.GetTile_V(pos);
			var tx = _local1.pos.x;
			var ty = _local1.pos.y;
			var txw = _local1.xw;
			var tyw = _local1.yw;
			var dx = (pos.x - tx);
			var dy = (pos.y - ty);
			if (0 < _local1.ID)
			{
				var px = ((txw + _local2.xw) - Math.abs(dx));
				var py = ((tyw + _local2.yw) - Math.abs(dy));
				if (px < py)
				{
					if (dx < 0)
					{
						px = px * -1;
						py = 0;
					}
					else
					{
						py = 0;
					}
				}
				else if (dy < 0)
				{
					px = 0;
					py = py * -1;
				}
				else
				{
					px = 0;
				}
				ProjAABB_Tile.ResolveBoxTile(px, py, _local2, _local1);
			}
			var crossV = false;
			var hitV = false;
			var dy = (pos.y - ty);
			var py = ((Math.abs(dy) + _local2.yw) - tyw);
			if (0 < py)
			{
				crossV = true;
				var eV;
				var nV;
				var pV;
				var signY;
				if (dy < 0)
				{
					eV = _local1.eU;
					nV = _local1.nU;
					pV = py;
					signY = 1;
				}
				else
				{
					eV = _local1.eD;
					nV = _local1.nD;
					pV = -py;
					signY = -1;
				}
				if (0 < eV)
				{
					if (eV == TileMapCell.EID_SOLID)
					{
						hitV = CollisionType.COL_AXIS;
						_local2.ReportCollisionVsWorld(0, pV, 0, signY, nV);
					}
					else
					{
						hitV = ProjAABB_Tile.ResolveBoxTile(0, pV, _local2, nV);
					}
				}
			}
			var crossH = false;
			var hitH = false;
			var dx = (pos.x - tx);
			var px = ((Math.abs(dx) + _local2.xw) - txw);
			if (0 < px)
			{
				crossH = true;
				var eH;
				var nH;
				var pH;
				var signX;
				if (dx < 0)
				{
					eH = _local1.eL;
					nH = _local1.nL;
					pH = px;
					signX = 1;
				}
				else
				{
					eH = _local1.eR;
					nH = _local1.nR;
					pH = -px;
					signX = -1;
				}
				if (0 < eH)
				{
					if (eH == TileMapCell.EID_SOLID)
					{
						hitH = CollisionType.COL_AXIS;
						_local2.ReportCollisionVsWorld(pH, 0, signX, 0, nH);
					}
					else
					{
						hitH = ProjAABB_Tile.ResolveBoxTile(pH, 0, _local2, nH);
					}
				}
			}
			if (((crossH && (hitH != CollisionType.COL_AXIS)) && (crossV)) && (hitV != CollisionType.COL_AXIS))
			{
				var dx = (pos.x - tx);
				var dy = (pos.y - ty);
				var px = ((Math.abs(dx) + _local2.xw) - txw);
				var py = ((Math.abs(dy) + _local2.yw) - tyw);
				var rx = 0;
				var ry = 0;
				var hit = false;
				var eH;
				var eV;
				var _local3;
				if ((dx < 0) && (dy < 0))
				{
					eH = _local1.nU.eL;
					eV = _local1.nL.eU;
					_local3 = _local1.nU.nL;
				}
				else if ((dx < 0) && (0 < dy))
				{
					eH = _local1.nD.eL;
					eV = _local1.nL.eD;
					_local3 = _local1.nD.nL;
				}
				else if ((0 < dx) && (0 < dy))
				{
					eH = _local1.nD.eR;
					eV = _local1.nR.eD;
					_local3 = _local1.nD.nR;
				}
				else if ((0 < dx) && (dy < 0))
				{
					eH = _local1.nU.eR;
					eV = _local1.nR.eU;
					_local3 = _local1.nU.nR;
				}
				var nx;
				var ny;
				if (px < py)
				{
					ry = (ny = 0);
					if (dx < 0)
					{
						rx = px;
						nx = 1;
					}
					else
					{
						rx = -px;
						nx = -1;
					}
				}
				else
				{
					rx = (nx = 0);
					if (dy < 0)
					{
						ry = py;
						ny = 1;
					}
					else
					{
						ry = -py;
						ny = -1;
					}
				}
				if (0 < eH)
				{
					if (0 < eV)
					{
						if (eH == TileMapCell.EID_SOLID)
						{
							if (eV == TileMapCell.EID_SOLID)
							{
								_local2.ReportCollisionVsWorld(rx, ry, nx, ny, _local3);
							}
							else
							{
								var temp = ProjAABB_Tile.ResolveBoxTile(rx, ry, _local2, _local3);
								if (temp == CollisionType.COL_NONE)
								{
									_local2.ReportCollisionVsWorld(pH, 0, signX, 0, _local3);
								}
							}
						}
						else if (eV == TileMapCell.EID_SOLID)
						{
							var temp = ProjAABB_Tile.ResolveBoxTile(rx, ry, _local2, _local3);
							if (temp == CollisionType.COL_NONE)
							{
								_local2.ReportCollisionVsWorld(0, pV, 0, signY, _local3);
							}
						}
						else
						{
							ProjAABB_Tile.ResolveBoxTile(rx, ry, _local2, _local3);
						}
					}
					else if (eH == TileMapCell.EID_SOLID)
					{
						_local2.ReportCollisionVsWorld(pH, 0, signX, 0, _local3);
					}
					else
					{
						ProjAABB_Tile.ResolveBoxTile(rx, ry, _local2, _local3);
					}
				}
				else if (0 < eV)
				{
					if (eV == TileMapCell.EID_SOLID)
					{
						_local2.ReportCollisionVsWorld(0, pV, 0, signY, _local3);
					}
					else
					{
						ProjAABB_Tile.ResolveBoxTile(rx, ry, _local2, _local3);
					}
					return;
				}
			}
		}

		public function CollideCirclevsTileMap(obj):void
		{
			var pos = obj.pos;
			var rad = obj.r;
			var _local1 = this.tiles.GetTile_V(pos);
			var tx = _local1.pos.x;
			var ty = _local1.pos.y;
			var txw = _local1.xw;
			var tyw = _local1.yw;
			var dx = (pos.x - tx);
			var dy = (pos.y - ty);
			if (0 < _local1.ID)
			{
				var px = ((txw + rad) - Math.abs(dx));
				var py = ((tyw + rad) - Math.abs(dy));
				ProjCircle_Tile.ResolveCircleTile(px, py, 0, 0, obj, _local1);
			}
			var crossV = false;
			var hitV = false;
			var dy = (pos.y - ty);
			var py = ((Math.abs(dy) + rad) - tyw);
			if (0 < py)
			{
				crossV = true;
				var eV;
				var nV;
				var oV;
				if (dy < 0)
				{
					eV = _local1.eU;
					nV = _local1.nU;
					oV = 1;
				}
				else
				{
					eV = _local1.eD;
					nV = _local1.nD;
					oV = -1;
				}
				if (0 < eV)
				{
					if (eV == TileMapCell.EID_SOLID)
					{
						hitV = CollisionType.COL_AXIS;
						obj.ReportCollisionVsWorld(0, py * oV, 0, oV, nV);
					}
					else
					{
						hitV = ProjCircle_Tile.ResolveCircleTile(0, py, 0, oV, obj, nV);
					}
				}
			}
			var crossH = false;
			var hitH = false;
			var dx = (pos.x - tx);
			var px = ((Math.abs(dx) + rad) - txw);
			if (0 < px)
			{
				crossH = true;
				var _local3;
				var nH;
				var oH;
				if (dx < 0)
				{
					_local3 = _local1.eL;
					nH = _local1.nL;
					oH = 1;
				}
				else
				{
					_local3 = _local1.eR;
					nH = _local1.nR;
					oH = -1;
				}
				if (0 < _local3)
				{
					if (_local3 == TileMapCell.EID_SOLID)
					{
						hitH = CollisionType.COL_AXIS;
						obj.ReportCollisionVsWorld(px * oH, 0, oH, 0, nH);
					}
					else
					{
						hitH = ProjCircle_Tile.ResolveCircleTile(px, 0, oH, 0, obj, nH);
					}
				}
			}
			if (((crossH && (hitH != CollisionType.COL_AXIS)) && (crossV)) && (hitV != CollisionType.COL_AXIS))
			{
				var _local2;
				var hit = false;
				if ((dx < 0) && (dy < 0))
				{
					var _local3 = _local1.nU.eL;
					eV = _local1.nL.eU;
					_local2 = _local1.nU.nL;
				}
				else if ((dx < 0) && (0 < dy))
				{
					var _local3 = _local1.nD.eL;
					eV = _local1.nL.eD;
					_local2 = _local1.nD.nL;
				}
				else if ((0 < dx) && (0 < dy))
				{
					var _local3 = _local1.nD.eR;
					eV = _local1.nR.eD;
					_local2 = _local1.nD.nR;
				}
				else if ((0 < dx) && (dy < 0))
				{
					var _local3 = _local1.nU.eR;
					eV = _local1.nR.eU;
					_local2 = _local1.nU.nR;
				}
				if (0 < (_local3 + eV))
				{
					if ((_local3 == TileMapCell.EID_SOLID) || (eV == TileMapCell.EID_SOLID))
					{
						var vx = (_local2.pos.x + (oH * _local2.xw));
						var vy = (_local2.pos.y + (oV * _local2.yw));
						var dx = (obj.pos.x - vx);
						var dy = (obj.pos.y - vy);
						var len = Math.sqrt((dx * dx) + (dy * dy));
						var pen = (obj.r - len);
						if (0 < pen)
						{
							if (len == 0)
							{
								dx = oH / Math.SQRT2;
								dy = oV / Math.SQRT2;
							}
							else
							{
								dx = dx / len;
								dy = dy / len;
							}
							obj.ReportCollisionVsWorld(dx * pen, dy * pen, dx, dy, _local2);
						}
					}
					else
					{
						dx = pos.x - _local2.pos.x;
						dy = pos.y - _local2.pos.y;
						px = (Math.abs(dx) + rad) - _local2.xw;
						py = (Math.abs(dy) + rad) - _local2.yw;
						ProjCircle_Tile.ResolveCircleTile(px, py, oH, oV, obj, _local2);
					}
					return;
				}
			}
		}

		public function QueryRayObj(out, p0, p1, obj):Boolean
		{
			var _local1 = out;
			var _local2 = p0;
			var c = this.tiles.GetTile_V(_local2);
			var i0 = c.i;
			var j0 = c.j;
			var dx = (p1.x - _local2.x);
			var _local3 = p1.y - _local2.y;
			var len = Math.sqrt((dx * dx) + (_local3 * _local3));
			if (len != 0)
			{
				dx = dx / len;
				_local3 = _local3 / len;
			}
			else
			{
				return(false);
			}
			var X = i0;
			var Y = j0;
			if (dx < 0)
			{
				var stepX = -1;
				var tMaxX = (((c.pos.x - c.xw) - _local2.x) / dx);
				var tDeltaX = ((2 * c.xw) / (-dx));
			}
			else if (0 < dx)
			{
				var stepX = 1;
				var tMaxX = (((c.pos.x + c.xw) - _local2.x) / dx);
				var tDeltaX = ((2 * c.xw) / dx);
			}
			else
			{
				var stepX = 0;
				var tMaxX = 100000000;
				var tDeltaX = 0;
			}
			if (_local3 < 0)
			{
				var stepY = -1;
				var tMaxY = (((c.pos.y - c.yw) - _local2.y) / _local3);
				var tDeltaY = ((2 * c.yw) / (-_local3));
			}
			else if (0 < _local3)
			{
				var stepY = 1;
				var tMaxY = (((c.pos.y + c.yw) - _local2.y) / _local3);
				var tDeltaY = ((2 * c.yw) / _local3);
			}
			else
			{
				var stepY = 0;
				var tMaxY = 100000000;
				var tDeltaY = 0;
			}
			var px = _local2.x;
			var py = _local2.y;
			if (TestRay_Tile.TestRayTile(_local1, px, py, dx, _local3, c))
			{
				var tx = _local1.x;
				var ty = _local1.y;
				if (TestRay_Tile.TestRay_Circle(_local1, _local2.x, _local2.y, dx, _local3, obj))
				{
					var dpO = (((_local2.x - _local1.x) * dx) + ((_local2.y - _local1.y) * _local3));
					var dpT = (((_local2.x - tx) * dx) + ((_local2.y - ty) * _local3));
					if (dpO < dpT)
					{
						_local1.x = tx;
						_local1.y = ty;
						return(false);
					}
					return(true);
				}
				else
				{
					_local1.x = tx;
					_local1.y = ty;
					return(false);
				}
			}
			var e;
			var n;
			while (c != null)
			{
				if (tMaxX < tMaxY)
				{
					if (stepX < 0)
					{
						e = c.eL;
						n = c.nL;
					}
					else
					{
						e = c.eR;
						n = c.nR;
					}
					if (0 < e)
					{
						px = _local2.x + (tMaxX * dx);
						py = _local2.y + (tMaxX * _local3);
						if (e == TileMapCell.EID_SOLID)
						{
							var tx = px;
							var ty = py;
							if (TestRay_Tile.TestRay_Circle(_local1, _local2.x, _local2.y, dx, _local3, obj))
							{
								var dpO = (((_local2.x - _local1.x) * dx) + ((_local2.y - _local1.y) * _local3));
								var dpT = (((_local2.x - tx) * dx) + ((_local2.y - ty) * _local3));
								if (dpO < dpT)
								{
									_local1.x = tx;
									_local1.y = ty;
									return(false);
								}
								return(true);
							}
							_local1.x = tx;
							_local1.y = ty;
							return(false);
						}
						else if (TestRay_Tile.TestRayTile(_local1, px, py, dx, _local3, n))
						{
							var tx = _local1.x;
							var ty = _local1.y;
							if (TestRay_Tile.TestRay_Circle(_local1, _local2.x, _local2.y, dx, _local3, obj))
							{
								var dpO = (((_local2.x - _local1.x) * dx) + ((_local2.y - _local1.y) * _local3));
								var dpT = (((_local2.x - tx) * dx) + ((_local2.y - ty) * _local3));
								if (dpO < dpT)
								{
									_local1.x = tx;
									_local1.y = ty;
									return(false);
								}
								return(true);
							}
							_local1.x = tx;
							_local1.y = ty;
							return(false);
						}
					}
					tMaxX = tMaxX + tDeltaX;
					X = X + stepX;
				}
				else
				{
					if (stepY < 0)
					{
						e = c.eU;
						n = c.nU;
					}
					else
					{
						e = c.eD;
						n = c.nD;
					}
					if (0 < e)
					{
						px = _local2.x + (tMaxY * dx);
						py = _local2.y + (tMaxY * _local3);
						if (e == TileMapCell.EID_SOLID)
						{
							var tx = px;
							var ty = py;
							if (TestRay_Tile.TestRay_Circle(_local1, _local2.x, _local2.y, dx, _local3, obj))
							{
								var dpO = (((_local2.x - _local1.x) * dx) + ((_local2.y - _local1.y) * _local3));
								var dpT = (((_local2.x - tx) * dx) + ((_local2.y - ty) * _local3));
								if (dpO < dpT)
								{
									_local1.x = tx;
									_local1.y = ty;
									return(false);
								}
								return(true);
							}
							_local1.x = tx;
							_local1.y = ty;
							return(false);
						}
						else if (TestRay_Tile.TestRayTile(_local1, px, py, dx, _local3, n))
						{
							var tx = _local1.x;
							var ty = _local1.y;
							if (TestRay_Tile.TestRay_Circle(_local1, _local2.x, _local2.y, dx, _local3, obj))
							{
								var dpO = (((_local2.x - _local1.x) * dx) + ((_local2.y - _local1.y) * _local3));
								var dpT = (((_local2.x - tx) * dx) + ((_local2.y - ty) * _local3));
								if (dpO < dpT)
								{
									_local1.x = tx;
									_local1.y = ty;
									return(false);
								}
								return(true);
							}
							_local1.x = tx;
							_local1.y = ty;
							return(false);
						}
					}
					tMaxY = tMaxY + tDeltaY;
					Y = Y + stepY;
				}
				c = n;
			}
			if (TestRay_Tile.TestRay_Circle(_local1, _local2.x, _local2.y, dx, _local3, obj))
			{
				return(true);
			}
			return(false);
			return(_local3);
		}

		public function CollideRayvsMap(out, p0, p1)
		{
			var _local1 = out;
			var _local2 = this.tiles.GetTile_V(p0);
			var i0 = _local2.i;
			var j0 = _local2.j;
			var dx = (p1.x - p0.x);
			var dy = (p1.y - p0.y);
			var len = Math.sqrt((dx * dx) + (dy * dy));
			if (len != 0)
			{
				dx = dx / len;
				dy = dy / len;
			}
			else
			{
				return(false);
			}
			if (dx < 0)
			{
				var stepX = -1;
				var tMaxX = (((_local2.pos.x - _local2.xw) - p0.x) / dx);
				var tDeltaX = ((2 * _local2.xw) / (-dx));
			}
			else if (0 < dx)
			{
				var stepX = 1;
				var tMaxX = (((_local2.pos.x + _local2.xw) - p0.x) / dx);
				var tDeltaX = ((2 * _local2.xw) / dx);
			}
			else
			{
				var stepX = 0;
				var tMaxX = 100000000;
				var tDeltaX = 0;
			}
			if (dy < 0)
			{
				var stepY = -1;
				var tMaxY = (((_local2.pos.y - _local2.yw) - p0.y) / dy);
				var tDeltaY = ((2 * _local2.yw) / (-dy));
			}
			else if (0 < dy)
			{
				var stepY = 1;
				var tMaxY = (((_local2.pos.y + _local2.yw) - p0.y) / dy);
				var tDeltaY = ((2 * _local2.yw) / dy);
			}
			else
			{
				var stepY = 0;
				var tMaxY = 100000000;
				var tDeltaY = 0;
			}
			var X = i0;
			var Y = j0;
			var ox;
			var oy;
			var px;
			var py;
			ox = (px = p0.x);
			oy = (py = p0.y);
			if (TestRay_Tile.TestRayTile(_local1, px, py, dx, dy, _local2))
			{
				return(true);
			}
			this.static_rend.SetStyle(0, 8947848, 100);
			var enter = new Vector2(0, 0);
			var exit = new Vector2(ox, oy);
			var e;
			var n;
			var _local3;
			var d;
			var dl;
			var dr;
			var vx;
			var vy;
			var hit_edge = false;
			var hit_tile = false;
			var done_waiting = false;
			var hit_objC = false;
			var hit_objD = false;
			var tileX;
			var tileY;
			var objX;
			var objY;
			var edgeX;
			var edgeY;
			while (_local2 != null)
			{
				enter.x = exit.x;
				enter.y = exit.y;
				if (tMaxX < tMaxY)
				{
					exit.x = ox + (tMaxX * dx);
					exit.y = oy + (tMaxX * dy);
					this.static_rend.DrawPlus(exit);
					if (stepX < 0)
					{
						e = _local2.eL;
						n = _local2.nL;
					}
					else
					{
						e = _local2.eR;
						n = _local2.nR;
					}
					if ((!hit_tile) && (0 < e))
					{
						if (e == TileMapCell.EID_SOLID)
						{
							_local1.x = exit.x;
							_local1.y = exit.y;
							hit_edge = true;
							edgeX = _local1.x;
							edgeY = _local1.y;
						}
						else if (TestRay_Tile.TestRayTile(_local1, exit.x, exit.y, dx, dy, n))
						{
							hit_tile = true;
							tileX = _local1.x;
							tileY = _local1.y;
						}
					}
					tMaxX = tMaxX + tDeltaX;
					X = X + stepX;
				}
				else
				{
					exit.x = ox + (tMaxY * dx);
					exit.y = oy + (tMaxY * dy);
					this.static_rend.DrawPlus(exit);
					if (stepY < 0)
					{
						e = _local2.eU;
						n = _local2.nU;
					}
					else
					{
						e = _local2.eD;
						n = _local2.nD;
					}
					if ((!hit_tile) && (0 < e))
					{
						if (e == TileMapCell.EID_SOLID)
						{
							_local1.x = exit.x;
							_local1.y = exit.y;
							hit_edge = true;
							edgeX = _local1.x;
							edgeY = _local1.y;
						}
						else if (TestRay_Tile.TestRayTile(_local1, exit.x, exit.y, dx, dy, n))
						{
							hit_tile = true;
							tileX = _local1.x;
							tileY = _local1.y;
						}
					}
					tMaxY = tMaxY + tDeltaY;
					Y = Y + stepY;
				}
				if (stepX < 0)
				{
					if (stepY < 0)
					{
						dr = _local2.nR.nU;
						dl = _local2.nL.nD;
					}
					else
					{
						dr = _local2.nL.nU;
						dl = _local2.nR.nD;
					}
				}
				else if (stepY < 0)
				{
					dr = _local2.nR.nD;
					dl = _local2.nL.nU;
				}
				else
				{
					dr = _local2.nL.nD;
					dl = _local2.nR.nU;
				}
				vx = enter.x - _local2.pos.x;
				vy = enter.y - _local2.pos.y;
				if (((vx * (-dy)) + (vy * dx)) < 0)
				{
					d = dl;
				}
				else
				{
					d = dr;
				}
				_local3 = _local2.next;
				var temp_obj = null;
				while (_local3 != null)
				{
					if (TestRayObj(_local1, ox, oy, dx, dy, _local3))
					{
						temp_obj = _local3;
						hit_objC = true;
						objX = _local1.x;
						objY = _local1.y;
						break;
					}
					_local3 = _local3.next;
				}
				_local3 = d.next;
				while (_local3 != null)
				{
					if (TestRayObj(_local1, ox, oy, dx, dy, _local3))
					{
						temp_obj = _local3;
						hit_objD = true;
						objX = _local1.x;
						objY = _local1.y;
						break;
					}
					_local3 = _local3.next;
				}
				if (hit_objC || (hit_objD))
				{
					_local1.x = objX;
					_local1.y = objY;
					temp_obj.pos.x = temp_obj.pos.x + (dx * 3);
					temp_obj.pos.y = temp_obj.pos.y + (dy * 3);
					return(true);
				}
				else if (done_waiting)
				{
					_local1.x = tileX;
					_local1.y = tileY;
					return(true);
				}
				else if (hit_edge)
				{
					_local1.x = edgeX;
					_local1.y = edgeY;
					return(true);
				}
				if (hit_tile)
				{
					done_waiting = true;
				}
				_local2 = n;
			}
			return(false);
			return(_local3);
		}

		public function CollideRayvsTiles(out, p0, p1)
		{
			var _local1 = this.tiles.GetTile_V(p0);
			var i0 = _local1.i;
			var j0 = _local1.j;
			var dx = (p1.x - p0.x);
			var dy = (p1.y - p0.y);
			var len = Math.sqrt((dx * dx) + (dy * dy));
			if (len != 0)
			{
				dx = dx / len;
				dy = dy / len;
			}
			else
			{
				return(false);
			}
			var X = i0;
			var Y = j0;
			if (dx < 0)
			{
				var stepX = -1;
				var tMaxX = (((_local1.pos.x - _local1.xw) - p0.x) / dx);
				var tDeltaX = ((2 * _local1.xw) / (-dx));
			}
			else if (0 < dx)
			{
				var stepX = 1;
				var tMaxX = (((_local1.pos.x + _local1.xw) - p0.x) / dx);
				var tDeltaX = ((2 * _local1.xw) / dx);
			}
			else
			{
				var stepX = 0;
				var tMaxX = 100000000;
				var tDeltaX = 0;
			}
			if (dy < 0)
			{
				var stepY = -1;
				var tMaxY = (((_local1.pos.y - _local1.yw) - p0.y) / dy);
				var tDeltaY = ((2 * _local1.yw) / (-dy));
			}
			else if (0 < dy)
			{
				var stepY = 1;
				var tMaxY = (((_local1.pos.y + _local1.yw) - p0.y) / dy);
				var tDeltaY = ((2 * _local1.yw) / dy);
			}
			else
			{
				var stepY = 0;
				var tMaxY = 100000000;
				var tDeltaY = 0;
			}
			var px = p0.x;
			var py = p0.y;
			if (TestRay_Tile.TestRayTile(out, px, py, dx, dy, _local1))
			{
				return(true);
			}
			var _local2;
			var _local3;
			while (_local1 != null)
			{
				if (tMaxX < tMaxY)
				{
					if (stepX < 0)
					{
						_local2 = _local1.eL;
						_local3 = _local1.nL;
					}
					else
					{
						_local2 = _local1.eR;
						_local3 = _local1.nR;
					}
					if (0 < _local2)
					{
						px = p0.x + (tMaxX * dx);
						py = p0.y + (tMaxX * dy);
						if (_local2 == TileMapCell.EID_SOLID)
						{
							out.x = px;
							out.y = py;
							return(true);
						}
						else if (TestRay_Tile.TestRayTile(out, px, py, dx, dy, _local3))
						{
							return(true);
						}
					}
					tMaxX = tMaxX + tDeltaX;
					X = X + stepX;
				}
				else
				{
					if (stepY < 0)
					{
						_local2 = _local1.eU;
						_local3 = _local1.nU;
					}
					else
					{
						_local2 = _local1.eD;
						_local3 = _local1.nD;
					}
					if (0 < _local2)
					{
						px = p0.x + (tMaxY * dx);
						py = p0.y + (tMaxY * dy);
						if (_local2 == TileMapCell.EID_SOLID)
						{
							out.x = px;
							out.y = py;
							return(true);
						}
						else if (TestRay_Tile.TestRayTile(out, px, py, dx, dy, _local3))
						{
							return(true);
						}
					}
					tMaxY = tMaxY + tDeltaY;
					Y = Y + stepY;
				}
				_local1 = _local3;
			}
			return(false);
			return(_local3);
		}

		public function TestRayObj(out, px, py, dx, dy, obj):Boolean
		{
			if (obj.OTYPE == OTYPE_AABB)
			{
				return TestRay_Tile.TestRay_AABB(out, px, py, dx, dy, obj);
			}
			return TestRay_Tile.TestRay_Circle(out, px, py, dx, dy, obj);
		}

		public function RotateAIDir(curDir, rot):Number
		{
			var _local1 = rot;
			if ((_local1 < 0) || (DroneObject.AI_ROT_270 < _local1))
			{
				return(curDir);
			}
			return((curDir + _local1) % 4);
		}

		public function DebugPointTest_Constant(x0, y0, x1, y1, n):void
		{
			if (n <= 0)
			{
				n = 1;
			}
			n = n + 1;
			var vx = (x1 - x0);
			var vy = (y1 - y0);
			var len = Math.sqrt((vx * vx) + (vy * vy));
			if (len == 0)
			{
				if (TestPoint_Tile.QueryPointvsTileMap(x0, y0))
				{
					this.debug_rend.SetStyle(2, 8921634, 100);
					this.debug_rend.DrawPlus_S(x0, y0);
				}
				return;
			}
			else
			{
				vx = vx / len;
				vy = vy / len;
			}
			var _local1 = 0;
			var _local3 = x0;
			var _local2 = y0;
			while (_local1 < len)
			{
				if (TestPoint_Tile.QueryPointvsTileMap(_local3, _local2))
				{
					this.debug_rend.SetStyle(2, 8921634, 100);
					this.debug_rend.DrawPlus_S(_local3, _local2);
				}
				_local1 = _local1 + n;
				_local3 = x0 + (_local1 * vx);
				_local2 = y0 + (_local1 * vy);
			}
			if (TestPoint_Tile.QueryPointvsTileMap(x1, y1))
			{
				this.debug_rend.SetStyle(2, 8921634, 100);
				this.debug_rend.DrawPlus_S(x1, y1);
			}
		}

		public function DebugPointTest(x0, y0, x1, y1, n):void
		{
			if (n <= 0)
			{
				n = 1;
			}
			n = n + 1;
			var x;
			var y;
			var _local1;
			var _local3;
			var _local2 = 0;
			while (_local2 < (n + 1))
			{
				_local1 = _local2 / n;
				_local3 = 1 - _local1;
				x = (_local1 * x0) + (_local3 * x1);
				y = (_local1 * y0) + (_local3 * y1);
				this.debug_rend.SetStyle(0, 0, 100);
				this.debug_rend.DrawPlus_S(x, y);
				if (TestPoint_Tile.QueryPointvsTileMap(x, y))
				{
					this.debug_rend.SetStyle(2, 8921634, 100);
					this.debug_rend.DrawCircle(new Vector2(x, y), 4);
				}
				_local2++;
			}
		}
	}
}